<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/01/16
 * Time: 下午4:58
 */

namespace Admin\Controller;
use Think\Controller;

class ArticleCategoryController extends Controller {


    /*
     * 列表
     */
    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $ArticleCategoryModel = M('ArticleCategory');

        $result = $ArticleCategoryModel->order('id desc')->select();

        if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*保存*/
    public function save(){

        $this->isAjaxLogin();//检查是否登录

        $ArticleCategoryModel = M('ArticleCategory');

        $id     = I('post.id/d');
        $name = I('post.name');
        $key = I('post.key');

        $data['name']   = $name;
        $data['key'] = $key;

        /*修改*/
        if($id){

            $result = $ArticleCategoryModel->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

        }
        /*添加*/
        else{

            $lastid = $ArticleCategoryModel->add($data);

            if($lastid !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据添加失败，请稍后在试！";
            }
        }

        $this->ajaxReturn($ajaxReturn);

    }

    /*
     * 详情
     */
    public function infos (){

        $this->isAjaxLogin();

        $id = I('post.id/d');

        $ArticleCategoryModel = M('ArticleCategory');

        $result = $ArticleCategoryModel->where('id='.$id)->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*删除*/
    public function delete(){

        $this->isAjaxLogin();//检查是否登录

        $id = I('post.id/d');

        $ArticleCategoryModel = M('ArticleCategory');
        $ArticleModel = M('Article');

        $ArticleModel->where('cid='.$id)->delete();
        $ArticleCategoryModel->where('id='.$id)->delete();
        
        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }


}